//
//  IntegralViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/5/11.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "IntegralViewController.h"
#import "HomeListTableViewCell.h"
#import "HomeNextHeaderView.h"
#import "BrandViewController.h"
#import "GoodInfoViewController.h"
#import "SearchViewController.h"
#import "ShopListViewController.h"
#import "ActionViewController.h"
#import "LoginViewController.h"
#import "BannerModel.h"
#import "GoodModel.h"
#import "HomeHotGoodModel.h"
#import "BuildingViewController.h"
#import "ShopGoodListViewController.h"
#import "GoodListViewController.h"

@interface IntegralViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) HomeNextHeaderView *headerView;
@property (nonatomic,strong) UIView *footerView;

@property (nonatomic,strong) NSArray *bannerArray;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) NSArray *hotArray;
@property (nonatomic,strong) NSArray *tocIdArray;
@property (nonatomic,strong) NSDictionary *dataDictionary;

@property (nonatomic,strong) NSString *page;

@end

//网络
@implementation IntegralViewController (NetWorking)

/**
 获取轮播图
 */
- (void)getBanner{
    [self showprogressHUD];
    NSDictionary *parameters = @{};
    [[AFAppDotNetAPIClient sharedClient] POST:@"AdvertisingCategory/selectTwo.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] integerValue] == 0) {
            self.bannerArray = responseObject[@"obj"];
            self.headerView.bannerArray = self.bannerArray;
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

/**
 下方热门商品
 */
- (void)getHotGoods{
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"ShopRecommendationMobile/selectTwoSR.action" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"ResponseStatus"] integerValue] == 0) {
            self.dataArray = responseObject[@"obj"];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (void)getSelectTwoById{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"tocId":self.tocId,
                                 };
    [[AFAppDotNetAPIClient sharedClient] POST:@"TitleTwoClassMobile/selectTwoById.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"ResponseStatus"] isEqualToString:@"0"]) {
            self.tocIdArray = responseObject[@"obj"];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

//UI
@implementation IntegralViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getBanner];
    [self getHotGoods];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:0 target:self action:@selector(searchEvent)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"vip"] style:0 target:self action:@selector(personInfoEvent)];
    self.navigationItem.titleView = ({
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 7, SCREEN_WIDTH, 30)];
        btn.layer.borderWidth = .5;
        btn.layer.borderColor = [UIColor whiteColor].CGColor;
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 15;
        [btn setTitle:@"搜索商品或店铺" forState:UIControlStateNormal];
        btn.titleLabel.font = DEF_FontSize_12;
        [btn addTarget:self action:@selector(searchEvent) forControlEvents:UIControlEventTouchUpInside];
        btn;
    });
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)setMenuArray:(NSArray *)menuArray{
//    _menuArray = menuArray;
    [self.view addSubview:self.tableView];
}

#pragma mark - Event

- (void)searchEvent{
    SearchViewController *vc = [SearchViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)personInfoEvent{
    [self.tabBarController setSelectedIndex:4];
}


#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeListTableViewCell class])];
    cell.dataDictionary = self.dataArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GoodListViewController *vc = [GoodListViewController new];
    vc.auId = self.dataArray[indexPath.row][@"s_id"];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - set & get

- (void)setTocId:(NSString *)tocId{
    _tocId = tocId;
    [self getSelectTwoById];
}

- (UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64-49) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footerView;
        _tableView.tableHeaderView = self.headerView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = 0;
        _tableView.rowHeight = 80;
        [_tableView registerClass:[HomeListTableViewCell class] forCellReuseIdentifier:NSStringFromClass([HomeListTableViewCell class])];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = @"1";
            [self getHotGoods];
        }];
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            self.page = [NSString stringWithFormat:@"%d",[self.page intValue]+1];
        //            [self getHotGoods];
        //        }];
    }
    return _tableView;
}

- (HomeNextHeaderView *)headerView{
    if (!_headerView) {
//        _headerView = [[HomeNextHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 38+265/WIDTH_5S_SCALE - 150 / WIDTH_5S_SCALE + (75 / WIDTH_5S_SCALE * (self.menuArray.count / 5 + (self.menuArray.count % 5 == 0 ? + 0 : + 1))))];
//        _headerView.menuArray = self.menuArray;
//        __block typeof(self) bSelf = self;
//        _headerView.menuHandle = ^(NSUInteger index){
//            ShopGoodListViewController *vc = [ShopGoodListViewController new];
//            vc.toc_id = [NSString stringWithFormat:@"%@",bSelf.tocIdArray[index][@"ttc_id"]];
//            vc.hidesBottomBarWhenPushed = YES;
//            vc.type = kShopGoodListViewControllerTypeTwo;
//            [bSelf.navigationController pushViewController:vc animated:NO];
//        };
//        _headerView.bannerHandle = ^(NSUInteger index) {
//            
//        };
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (NSDictionary *)dataDictionary{
    if (!_dataDictionary) {
        _dataDictionary = @{};
    }
    return _dataDictionary;
}

- (NSString *)page{
    if (!_page) {
        _page = @"1";
    }
    return _page;
}

- (NSArray *)tocIdArray{
    if (!_tocIdArray) {
        _tocIdArray = @[];
    }
    return _tocIdArray;
}


@end
